
'''	
	This file is part of Roseena.
	
	Copyright (C) 2008	Mohamed Hazem <mohamed.hazem@student.guc.edu.eg>
	Copyright (C) 2008	Mostafa Hany <mostafa.hany@student.guc.edu.eg>

	
	Roseena is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Roseena is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Roseena.	If not, see <http://www.gnu.org/licenses/>.
'''

from PyQt4.QtCore import SIGNAL
from PyQt4.QtCore import QDataStream, QString
from PyQt4.QtNetwork import QTcpSocket

SIZEOF_UINT16 = 2

class Socket(QTcpSocket):
	def __init__(self, parent=None):
		QTcpSocket.__init__(self, parent)

		self.connect(self, SIGNAL("readyRead()"), self.readRequest)
		self.connect(self, SIGNAL("disconnected()"), self.dc)

		self.nextBlockSize = 0

		self.requestString = None

	def dc(self):
		print "disconnected"

	def readRequest(self):
		stream = QDataStream(self)
		stream.setVersion(QDataStream.Qt_4_4)

		if self.nextBlockSize == 0:
			if self.bytesAvailable() < SIZEOF_UINT16:
				return
			self.nextBlockSize = stream.readUInt16()

		if self.bytesAvailable() < self.nextBlockSize:
			return

		self.requestString = QString()

		stream >> self.requestString
		print "request string " + self.requestString

		requestList = self.requestString.split(" ")

		if requestList[0] == "GET":
			print "IT'S A GET"
			self.Server.gotConnection()
		elif requestList[0] == "BYE":
			self.disconnectFromHost()
			print "IT'S A BYE"
		else:
			print "INVALID REQUEST"

	def sendReply(self,):
		reply = QByteArray()
